import express from 'express';
import * as articles from "../../controllers/articles.js";
import * as category from "../../controllers/categories.js";
import * as comments from "../../controllers/comments.js";


const router = express.Router();
//1
router.get('/search', async function (req, res) {
  let page = req.query.page;
  let perpage = req.query.perpage;
  if (page == null || page == "") {
    page = 1;
  }
  if (perpage == null || perpage == "") {
    perpage = 6;
  }
  let startindex = (page - 1) * perpage;
  let type = req.query.type;
  let key = req.query.key;
  let data = await articles.selectArticlesGetCount(startindex, perpage, type, key);

  for (let articles of data) {
    let str = articles.content.replace(/<\/?[^>]+(>|$)/g, "").substring(0, 500);
    articles.content = str;
  }

  res.send({
    code: 200,
    mas: "恭喜啊，兄弟，文章搜索获取成功😀",
    data
  })
})
//2
router.get("/category", async function (req, res) {
  let data = await category.selectCategories();
  res.send({
    code: 200,
    msg: "恭喜啊，兄弟，文章类型获取成功😀",
    data
  })
});
//3
router.get("/hotpic", async function (req, res) {
  let [{ count }] = await articles.selectArticlescount();
  let start = Math.floor(Math.random() * count - 5);
  let data = await articles.selectArticlesRandom(start);
  res.send({
    code: 200,
    msg: "恭喜啊，兄弟，热点图片获取成功😀",
    data
  })
});
//4
router.get("/rank", async function (req, res) {
  let data = await articles.selectArticlesHot();
  res.send({
    code: 200,
    msg: "恭喜啊，兄弟，热门文章获取成功😀",
    data
  })
});
//5
router.get("/latest", async function (req, res) {
  let data = await articles.selectArticlesNew();
  res.send({
    code: 200,
    msg: "恭喜啊，兄弟，最新资讯获取成功😀",
    data
  })
});
//6
router.get("/latest_comment", async function (req, res) {
  let data = await comments.selectCommentsNew();
  res.send({
    code: 200,
    msg: "恭喜啊，兄弟，最新评论获取成功😀",
    data
  })
});
//7
router.get("/attention", async function (req, res) {
  let [{ count }] = await articles.selectArticlescount();
  let start = Math.floor(Math.random() * count - 5);
  let data = await articles.selectArticlesContent(start);
  res.send({
    code: 200,
    msg: "恭喜啊，兄弟，焦点关注获取成功😀",
    data
  })
})

//8
router.get('/article', async function (req, res) {
  let id = req.query.id;
  if (id == null || id == "") {
    res.send({
      code: 400,
      msg: `参数错误，id${req.id}不正确`
    })
    return;
  };

  let [data] = await articles.selectArticlesCountId(id);
  if (data == null) {
    res.send({
      code: 400,
      msg: '兄弟啊，找不着介个文章啊'
    })
    return;
  };

  let [preData] = await articles.selectArticlesPre(id);
  let [nextData] = await articles.selectArticlesNext(id);

  data.prev = preData == undefined ? null : preData;
  data.next = nextData == undefined ? null : nextData;

  res.send({
    code: 200,
    msg: "恭喜啊，兄弟，文章详细内容获取成功😀",
    data
  })
})

//9
router.post('/post_comment', async function (req, res) {
  let author = req.body?.author ?? "";
  let content = req.body?.content ?? "";
  let articleId = req.body?.articleId ?? "";

  let [{ count }] = await articles.selectArticlescount();

  if (author == "" || content == "" || articleId == "" || articleId < 1 || articleId > count) {
    res.send({
      code: 400,
      msg: "兄弟，上传参数不对啊QAQ"
    })
    return;
  }
  await comments.insertcomments(author, content, articleId);
  res.send({
    code: 201,
    msg: "恭喜啊，兄弟，评论成功😀"
  })
})

//10
router.get("/get_comment", async (req, res) => {
  const id = req.query.articleId;
  if (id == null || id == "") {
    res.send({
      code: 400,
      msg: `参数错误，请求参数为 ${req.id}`
    })
    return;
  };
  let data = await comments.selectCommentsAll(id);
  res.send({
    code: 200,
    msg: "查询成功",
    data
  })
});

export default router;
